package cn.wen.yinxiu.member.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.common.to.system.SearchHistoryTO;
import cn.wen.yinxiu.common.toolkit.StringUtils;
import cn.wen.yinxiu.member.vo.SearchHistoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.member.dao.SearchHistoryDao;
import cn.wen.yinxiu.member.entity.SearchHistoryEntity;
import cn.wen.yinxiu.member.service.SearchHistoryService;


@Service("searchHistoryService")
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryDao, SearchHistoryEntity> implements SearchHistoryService {

    @Autowired
    private SearchHistoryDao searchHistoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SearchHistoryEntity> page = this.page(
                new Query<SearchHistoryEntity>().getPage(params),
                new QueryWrapper<SearchHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SearchHistoryVO> getSearchHistory(Long userId, String browserId) {
        return searchHistoryDao.querySearchHistory(userId, browserId,
                StatusEnum.NORMAL_USE.code(), DelEnum.NORMAL.code());
    }

    @Override
    public boolean deleteSearchHistory(Long searchHistoryId) {
        SearchHistoryEntity target = new SearchHistoryEntity();
        target.setIsDeleted(DelEnum.DELETE.code());
        target.setId(searchHistoryId);
        return searchHistoryDao.updateById(target) > 0;
    }

    @Override
    public boolean clearSearchHistory(Long userId, String browserId) {
        QueryWrapper<SearchHistoryEntity> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        if (StringUtils.isNotEmpty(browserId)) {
            queryWrapper.eq("browser_id", browserId);
        }
        SearchHistoryEntity target = new SearchHistoryEntity();
        target.setIsDeleted(DelEnum.DELETE.code());
        return searchHistoryDao.update(target, queryWrapper) > 0;
    }

    @Override
    public boolean addSearchHistory(SearchHistoryTO searchHistoryTO) {
        // 1.需要先删除其中的相同记录
        QueryWrapper<SearchHistoryEntity> queryWrapper = new QueryWrapper<>();
        if (searchHistoryTO.getUserId() != null) {
            queryWrapper.eq("user_id", searchHistoryTO.getUserId());
        }
        if (StringUtils.isNotEmpty(searchHistoryTO.getBrowserId())) {
            queryWrapper.eq("browser_id", searchHistoryTO.getBrowserId());
        }
        queryWrapper.eq("keyword", searchHistoryTO.getKeyword());
        searchHistoryDao.delete(queryWrapper);
        // 2.添加记录
        SearchHistoryEntity target = new SearchHistoryEntity();
        BeanUtils.copyProperties(searchHistoryTO, target);
        target.setIsDeleted(DelEnum.NORMAL.code());
        target.setStatus(StatusEnum.NORMAL_USE.code());
        return searchHistoryDao.insert(target) > 0;
    }

}